/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ceair.flight.refund.service.cronjob.validator;

import com.ceair.flight.refund.dto.appmoblie.RefundApplyDto;
import com.ceair.flight.refund.dto.appmoblie.RefundTktDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Objects;

/**
 *
 * @author youguilin
 */
@Service("basicInvoluntaryValidator")
public class BasicInvoluntaryValidator extends BaseValidator {

    private static final Logger LOG = LoggerFactory.getLogger(BasicInvoluntaryValidator.class);

    /**
     *
     * @param refundApply
     * @return
     */
    @Override
    public boolean validate(RefundApplyDto refundApply) {
        return super.validate(refundApply) && verifyData(refundApply) && checkRefundFee(refundApply) && checkRefundPoints(refundApply);
    }

    boolean checkRefundFee(RefundApplyDto refundApply) {
        BigDecimal refundFee = refundApply.getRefundFee();
        RefundTktDto refundTkt = refundApply.getRefundTkt();
        BigDecimal tktRefundFee = refundTkt.getRefundFee();
        return refundFee.compareTo(BigDecimal.ZERO) == 0 && refundFee.compareTo(tktRefundFee) == 0;
    }

    /**
     * 积分全退
     *
     * @param refundApply
     * @return
     */
    boolean checkRefundPoints(RefundApplyDto refundApply) {
        Long refundPoints = refundApply.getRefundPoints();

        RefundTktDto refundTkt = refundApply.getRefundTkt();
        Long tktPoints = refundTkt.getTktPoints();

        Long tktRefundPoints = refundTkt.getRefundPoints();
        //如果是积分票, 则退全部积分
        if (tktPoints != null && tktPoints > 0) {
            return refundPoints != null && Objects.equals(refundPoints, tktPoints) && Objects.equals(refundPoints, tktRefundPoints);
        }
        //如果不是积分票, 但是退的积分大于0, 检查则失败
        return (refundPoints == null || refundPoints == 0) && (tktRefundPoints == null || tktRefundPoints == 0);
    }

    boolean verifyData(RefundApplyDto refundApply) {
        BigDecimal refundAmt = refundApply.getRefundAmount();
        RefundTktDto refundTkt = refundApply.getRefundTkt();
        BigDecimal tktRefundAmt = refundTkt.getRefundAmt();
        BigDecimal refundTax = refundTkt.getRefundTax();
        BigDecimal tktSalePrice = refundTkt.getTktAmt();
        BigDecimal facePrice = refundTkt.getTktPrice();
        BigDecimal leftPrice = refundTkt.getLeftTktPrice();
        BigDecimal promoteAmt = super.getPromoteAmt(refundApply);
        BigDecimal faceGap = super.getFaceGap(refundApply);
        BigDecimal taxGap = super.getTaxGap(refundApply);
        return tktRefundAmt.compareTo(BigDecimal.ZERO) > 0
                && refundTax.compareTo(BigDecimal.ZERO) > 0
                && leftPrice.compareTo(BigDecimal.ZERO) > 0
                && tktSalePrice.add(faceGap).add(taxGap).subtract(promoteAmt).compareTo(refundAmt) == 0
                && facePrice.subtract(promoteAmt).compareTo(tktRefundAmt) == 0;
    }
}
